package com.xucy.springboot.common;

/**
 * @Author xucy
 * @Date 2019-03-26 15:51
 * @Description 简单排序--顺序排序，插入排序，查找排序
 **/

public class EasySort {
    /**
     * 将小的数向左移动的冒泡排序
     *
     * @param
     * @return
     * @Author xucy
     * @Date 15:51 2019-03-26
     */
    public static long[] mpSort(long[] arr) {
        long temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j] < arr[j - 1]) {
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }

    /**
     * 将大的数向右移的冒泡排序
     *
     * @param
     * @return
     * @Author xucy
     * @Date 19:11 2019-03-26
     */
    public static long[] mpSort1(long[] arr) {
        long temp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }

    /**
     * 选择排序
     *
     * @param
     * @return
     * @Author xucy
     * @Date 17:07 2019-03-26
     */
    public static long[] selectSort(long[] arr) {

        long temp = 0;
        int k = 0;
        for (int i = 0; i < arr.length; i++) {
            k = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[k] > arr[j]) {
                    k = j;
                }
            }

            temp = arr[k];
            arr[k] = arr[i];
            arr[i] = temp;
        }

        return arr;
    }

    /**
     * 插入排序
     *
     * @param
     * @return
     * @Author xucy
     * @Date 19:11 2019-03-26
     */
    public static long[] insertSort(long[] arr) {

        long temp = 0;
        for (int i = 1; i < arr.length; i++) {
            temp = arr[i];
            int j = i;
            while (j > 0 && arr[j - 1] >= temp) {
                arr[j] = arr[j - 1];
                j--;
            }
            arr[j] = temp;
        }


        return arr;
    }

    public static void main(String[] args) {
        long[] arr = new long[5];
        arr[0] = 23;
        arr[1] = 21;
        arr[2] = 13;
        arr[3] = 12;
        arr[4] = 25;

        long[] newArr = insertSort(arr);
        for (int i = 0; i < newArr.length; i++) {
            System.out.print(newArr[i] + ",");
        }

    }
}
